//
//  CWStatusCell.swift
//  新浪微博练习02
//
//  Created by 曹魏 on 16/10/5.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

let commonMargin: CGFloat = 8
//图片之间的间距
private let pictureCellMargin: CGFloat = 3
//计算图片的宽度
private let maxWidth = ScreenWidth - 2 * commonMargin
private let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3
class CWStatusCell: UITableViewCell {
    
    
    @IBOutlet weak var iconView: UIImageView!
    
    
    
    @IBOutlet weak var nameLabel: UILabel!
    
    
    
    @IBOutlet weak var memberImage: UIImageView!
    
    @IBOutlet weak var contentLabel: UILabel!
    
    
    @IBOutlet weak var timeLabel: UILabel!
    
    
    @IBOutlet weak var sourceLabel: UILabel!

    
    @IBOutlet weak var avatarImage: UIImageView!
    
    
    @IBOutlet weak var pictureView: CWPictureView!
    
    
    @IBOutlet weak var pictureViewWidthCons: NSLayoutConstraint!
    
    
    @IBOutlet weak var pictureViewHeightCons: NSLayoutConstraint!
    
    
    @IBOutlet weak var pictureViewLayout: UICollectionViewFlowLayout!
    
    
    @IBOutlet weak var pictureViewTopCons: NSLayoutConstraint!
    //绑定数据
    var viewmodel : CWStatusViewModel? {
        didSet {
            //设置视图的数据
            //iconView.sd_setImage(with: <#T##URL!#>)
            iconView.sd_setImage(with: viewmodel?.iconURL)
            timeLabel.text = viewmodel?.status?.created_at
            sourceLabel.text = viewmodel?.status?.source
            contentLabel.text = viewmodel?.status?.text
            avatarImage.image = viewmodel?.avatarImage
            memberImage.image = viewmodel?.memberImage
            nameLabel.text = viewmodel?.status?.user?.name
            //一旦设置了数据 就应该立即更新配图视图的大小的约束
//            let count = viewmodel?.status?.pic_urls?.count ?? 0
//            let pSize = changePictureViewSize(count: count)
            //更新约束值
//            pictureViewWidthCons.constant = pSize.width
//            pictureViewHeightCons.constant = pSize.height
            //根据是否有配图调整顶部间距
//            pictureViewTopCons.constant = (count == 0 ? 0 : commonMargin)
            //给配图视图设置数据源
//            pictureView.pictureInfos = viewmodel?.status?.pic_urls
            
        }
    }
    
    
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
//        self.contentView.backgroundColor = UIColor.orange
        //设置contentLabel的最大布局宽度
        //设置cell的选中样式
        selectionStyle = .none
        print(self.bounds)
        print("~~~~~~~~~~~~~~~~~~~~~~")
        //能够在viewdidLoad或者awakeFromNib中完成设置就尽量不要放在layoutSubViews和viewDidLayoutsubviews方法中实现
        contentLabel.preferredMaxLayoutWidth = ScreenWidth - 2 * commonMargin
        
        
        
        //设置配图视图的布局对象
        pictureViewLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //设置间距
        //行间距 默认的间距: 10
        pictureViewLayout.minimumLineSpacing = pictureCellMargin
        pictureViewLayout.minimumInteritemSpacing = pictureCellMargin
    }
    
    //根据图片的张数来计算配图视图的大小
    private func changePictureViewSize(count: Int) -> CGSize {
        
        //0张图片
        if count == 0 {
            return CGSize.zero
        }
        //4 张图片
        if count == 4 {
            let width = itemWidth * 2 + pictureCellMargin
            return CGSize(width: width, height: width)
        }
        //其他 1,2,3,5,6,7,8,9  -> 3 * n
        //已知个数 和 列数 来计算 函数
        let rowCount = CGFloat((count - 1) / 3 + 1)
        let height = rowCount * itemWidth + (rowCount - 1) * pictureCellMargin
        return CGSize(width: maxWidth, height: height)
    }
    
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        print("哈哈哈哈哈哈哈")
        print("~~~~~~~~~~~~~~~~~~~~~~")
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
